--[===================================================================[--

Digiplay - Copyright © 2017 Pedro Gimeno Fortea.

This file is part of the package Digiplay. Both copying and distribution
of this package are permitted, with or without modification, under the
sole condition that any copyright notices and this notice are preserved.
This package is offered as-is, without any warranty express or implied.

--]===================================================================]--

-- Informative note: The textures included in the package are under the
-- same license; the files include a valid tEXt chunk with the copyright
-- and license notice.

-------------------------------------------------------------------------

-- Minetest 0.4.16+ required for colour changing.

-------------------------------------------------------------------------


local function multicolour_light_on_punch(pos, node, puncher, pointed_thing)
  local channel = minetest.get_meta(pos):get_string("digich")
  if channel == "" then
    return
  end
  digiline:receptor_send(pos, digiline.rules.default, channel, "L " .. puncher:get_player_name())
end

local function multicolour_light_on_rightclick(pos, node, clicker, itemstack,
                                               pointed_thing)
  local channel = minetest.get_meta(pos):get_string("digich")
  if channel == "" then
    return
  end
  digiline:receptor_send(pos, digiline.rules.default, channel, "R " .. clicker:get_player_name())
end

local function multicolour_light_on_construct(pos)
  local meta = minetest.get_meta(pos)
  meta:set_string("formspec", "field[channel;Digiline Channel;]")
end

local function multicolour_light_on_receive_fields(pos, formname, fields, sender)
  if fields.channel and fields.channel ~= "" then
    local meta = minetest.get_meta(pos)
    meta:set_string("formspec", "")
    meta:set_string("digich", fields.channel)
  end
end

local function multicolour_light_effector_action(pos, node, channel, msg)
  local configured_channel = minetest.get_meta(pos):get_string("digich")
  if channel == configured_channel and type(msg) == "string" then
    local node = minetest.get_node(pos)
    local onoff, colour = msg:match("^([^#]*)#?(%x-)$")
    if onoff == "off" and node.name == "digiplay:multicolour_light_on" then
      node.name = "digiplay:multicolour_light"
    elseif onoff == "on" and node.name == "digiplay:multicolour_light" then
      node.name = "digiplay:multicolour_light_on"
    end
    if colour ~= "" and colour ~= nil then
      local base
      if colour:match("^%x%x%x%x%x%x$") then
        base = 256
      elseif colour:match("^%x%x%x$") then
        base = 16
      end

      if base then
        local r = tonumber(colour, 16)
        local b = r % base
        r = (r - b) / base
        local g = r % base
        r = (r - g) / base
        node.param2 = math.floor(g*5/base)*25 + math.floor(r*5/base)*5 + math.floor(b*5/base) + 1
        if node.param2 == 125 then node.param2 = 0 end
      end
    end
    minetest.swap_node(pos, node)
  end
end


for i = 0, 1 do
  local onoff = i == 0 and "" or "_on"
  local node_definition = {
    description = "Multi-coloured light";
    tiles = {"digiplay_multicolour_light" .. onoff .. ".png"};
    groups = {dig_immediate = 2};

    drawtype = "color";
    paramtype2 = "color";
    palette = "digiplay_palette.png";
    place_param2 = 0;

    on_receive_fields = multicolour_light_on_receive_fields;
    on_punch = multicolour_light_on_punch;
    on_construct = multicolour_light_on_construct;
    on_rightclick = multicolour_light_on_rightclick;
    digiline = {
      receptor = {
        rules = digiline.rules.default;
      };
      effector = {
        rules = digiline.rules.default;
        action = multicolour_light_effector_action;
      };
    };
  }

  if i == 1 then
    node_definition.light_source = 12
    node_definition.drop = "digiplay:multicolour_light"
    node_definition.groups.not_in_creative_inventory = 1
  end
  minetest.register_node("digiplay:multicolour_light" .. onoff, node_definition)
end

minetest.register_craft {
  output = "digiplay:multicolour_light";
  recipe = {
    {"", "digilines:wire_std_00000000", ""},
    {"default:clay", "default:mese_crystal_fragment", "default:clay"},
    {"dye:red", "dye:green", "dye:blue"}
  };
}

print("[mod] digiplay successfully loaded!")
